/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.JLabel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.constants.ViewConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.notation.Notation;

/**
 *
 * @author Rik Bauwens
 */
public class PerformancePanel extends NotationPanel
{
        public PerformancePanel()
        {
                super(LanguageResource.getInstance().getLabel(LabelConstants.PERFORMANCE));
        }

        @Override
        protected void init()
        {
                JLabel bpmLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.BPM));
                addLabel(bpmLabel);
                final SpinnerModel bpmModel =
                        new SpinnerNumberModel(Notation.getInstance().getBPM(),
                                               ViewConstants.MIN_BPM,
                                               ViewConstants.MAX_BPM,
                                               ViewConstants.STEP_BPM);
                final CustomSpinner bpm = new CustomSpinner(bpmModel).setEditable(true);
                Notation.getInstance().getBPMNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) bpmModel.getValue()).intValue() != Notation.getInstance().getBPM())
                                        bpmModel.setValue(Notation.getInstance().getBPM());
                        }
                });
                bpmModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getBPM() != ((Integer) bpmModel.getValue()).intValue())
                                        Notation.getInstance().setBPM((Integer) bpmModel.getValue());
                        }
                });
                bpm.addFocusListener(new FocusListener()
                {
                        public void focusGained(FocusEvent e)
                        {
                                Notation.getInstance().clearSelectedElements();
                        }

                        public void focusLost(FocusEvent e) {}
                });
                bpm.setMaximumSize(bpm.getPreferredSize());
                addOption(bpm);
                addSpacing();

                JLabel instrumentLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.INSTRUMENT));
                addLabel(instrumentLabel);
                final InstrumentChooser instrumentChooser = new InstrumentChooser();
                instrumentChooser.addFocusListener(new FocusListener()
                {
                        public void focusGained(FocusEvent e)
                        {
                                Notation.getInstance().clearSelectedElements();
                        }

                        public void focusLost(FocusEvent e) {}
                });
                Notation.getInstance().getInstrumentNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(instrumentChooser.getInstrument().compareTo(Notation.getInstance().getInstrument()) != 0)
                                        instrumentChooser.setInstrument(Notation.getInstance().getInstrument());
                        }
                });
                instrumentChooser.addActionListener(new ActionListener()
                {
                        public void actionPerformed(ActionEvent e)
                        {
                                if(!instrumentChooser.isActionValid())
                                        return;
                                if(Notation.getInstance().getInstrument().compareTo(instrumentChooser.getInstrument()) != 0)
                                        Notation.getInstance().setInstrument(instrumentChooser.getInstrument());
                        }
                });
                instrumentChooser.setInstrument(Notation.getInstance().getInstrument());
                addOption(instrumentChooser);
        }
}
